package com.gitee.aurora.server.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.server.vo.*;
import com.gitee.aurora.server.service.PtConfigService;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ptConfig")
@Api(value = "平台组件配置类", tags = {"平台组件配置管理类"})
public class PtConfigController {
    @Autowired
    private PtConfigService ptConfigService;

    /**
     *获取检测机构树
     */
    @GetMapping("/ptTree")
    @ApiOperation(value = "获取检测机构树", notes = "获取检测机构树")
    public ResultVO<List<PtcjConfig>> ptTree()  {
        List<PtcjConfig> treeList= null;
        try {
            treeList = ptConfigService.queryPtcjConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVOUtils.success(treeList);
    }
    /**
     *查询检测机构配置
     */
    @PostMapping("/queryPtConfig")
    @ApiOperation(value = "查询检测机构配置", notes = "查询检测机构配置")
    public ResultVO<Pagination> queryPtConfig(PtConfig ptConfig, PageParam pageParam) {
        Pagination pagination= null;
        try {
            pagination = ptConfigService.queryPtConfigService(ptConfig,pageParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(pagination);
    }

    /**
     *查询检测数据表下拉列表
     */
    @GetMapping("/queryPtTableConfig")
    @ApiOperation(value = "查询检测数据表下拉列表", notes = "查询检测数据表下拉列表")
    public ResultVO<List<TableConfig>> queryPtTableConfig()  {
        List<TableConfig> pList= null;
        try {
            pList = ptConfigService.queryTable();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(pList);
    }

    /**
     *查询平台类型
     */
    @GetMapping("/queryPtLx")
    @ApiOperation(value = "查询平台类型", notes = "查询平台类型")
    public ResultVO<List<SqlConfigItem>> queryPtLx() {
        List<SqlConfigItem> pList= null;
        try {
            pList = ptConfigService.queryPtLx();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(pList);
    }

    /**
     *查询平台系列
     */
    @GetMapping("/queryPtXl")
    @ApiOperation(value = "查询平台系列", notes = "查询平台系列")
    public ResultVO<List<SqlConfigItem>> queryPtXl() {
        List<SqlConfigItem> pList= null;
        try {
            pList = ptConfigService.queryPtXl();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(pList);
    }
    /**
     *删除平台配置数据
     */
    @PostMapping("/deletePtConfig")
    @ApiOperation(value = "删除平台配置数据", notes = "删除平台配置数据")
    public ResultVO deletePtConfig(PtConfig ptConfig) {
        int result=0;
        try {
            result=ptConfigService.deletePtConfigService(ptConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(result);
    }
    /**
     *修改平台配置数据
     */
    @PostMapping("/updatePtConfig")
    @ApiOperation(value = "修改平台配置数据", notes = "修改平台配置数据")
    public ResultVO updatePtConfig(PtConfig ptConfig) {
        /* List<SqlConfigItem> pList= null;*/
        int result=0;
        try {
            result= ptConfigService.updatePtConfigService(ptConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(result);
    }
    /**
     *新增平台配置数据
     */
    @PostMapping("/insertPtConfig")
    @ApiOperation(value = "新增平台配置数据", notes = "新增平台配置数据")
    public ResultVO insertPtConfig(PtConfig ptConfig) {
        /* List<SqlConfigItem> pList= null;*/
        int result=0;
        try {
            result= ptConfigService.insertPtConfigService(ptConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(result);
    }


    /**
     *查询平台下级组件
     */
    @GetMapping("/queryPtxjInfo")
    @ApiOperation(value = "查询平台下级组件", notes = "查询平台下级组件")
    public ResultVO queryPtxjInfo(String  sCode) {
        List<PtcjConfig> pList= null;

        try {
            pList = ptConfigService.queryPtxjInfo(sCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(pList);
    }
    /**
     *根据表名查询动态列名
     */
    @PostMapping("/querySql")
    @ApiOperation(value = "根据表名查询动态列名", notes = "根据表名查询动态列名")
    public ResultVO querySql(@RequestBody PtConfig ptConfig) throws JsonProcessingException {
        List<PtConfig> pList= null;
        List list=new ArrayList();
        ObjectMapper mapper=new ObjectMapper();

        try {
          pList= ptConfigService.querySql(ptConfig);
          if(pList.size()>0) {
              list = mapper.readValue(pList.get(0).getJcColunmn(), List.class);
          }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResultVOUtils.success(list);
    }





}
